package com.swak.hazelcast;

import java.util.concurrent.atomic.AtomicBoolean;

import com.hazelcast.cp.ISemaphore;
import com.swak.lock.Lock;

public class HazelcastLock implements Lock {

	private final ISemaphore semaphore;
	private final AtomicBoolean released = new AtomicBoolean();

	public HazelcastLock(ISemaphore semaphore) {
		this.semaphore = semaphore;
	}

	@Override
	public String name() {
		return null;
	}

	/**
	 * 占用锁，如果失败则抛出异常
	 */
	public void lock() {
	}

	/**
	 * 释放锁 -- 特殊情况下，一般不需要执行
	 *
	 * @return 是否释放
	 */
	public void unlock() {
		if (released.compareAndSet(false, true)) {
			semaphore.release();
		}
	}
}
